package com.toymate.ai.asr.model.baidu;

import com.baidu.aip.speech.AipSpeech;
import com.toymate.ai.asr.AbstractSttService;
import com.toymate.ai.asr.AsrConfig;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Optional;

/**
 * BaiduSttService
 *
 * @author weirdor (179946422@qq.com)
 * @version v1.0.0
 */
@Slf4j
public class BaiduSttService extends AbstractSttService {

    public BaiduSttService(AsrConfig config) {
        this.config = config;
    }

    @Override
    public Optional<String> discern(String id, String filePath) {
        String appId = config.getBaiduAsrConfig().getAppId();
        String apiKey = config.getBaiduAsrConfig().getApiKey();
        String secretKey = config.getBaiduAsrConfig().getSecretKey();
        AipSpeech client = new AipSpeech(appId, apiKey, secretKey);
        org.json.JSONObject asrRes = client.asr(filePath, "wav", 16000, new HashMap<>());
        if (asrRes.getInt("err_no") == 0) {
            return Optional.of(asrRes.getJSONArray("result").get(0).toString());
        }
        return Optional.empty();
    }

}
